<template>
  <el-dialog :title="data.title" :visible.sync="data.visible" width="550px">
    <el-form label-width="80px" ref="ruleForm" :model="ruleForm" :rules="rules">
      <el-form-item label="通知标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="概述" prop="overview">
        <el-input v-model="ruleForm.overview" type="textarea" maxlength="400" show-word-limit :autosize="{ minRows: 5, maxRows:8}"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="send">{{ editData ? '更新' : '立即发送' }}</el-button>
        <el-button @click="resetForm('ruleForm')" v-if="!editData">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: "InformForm",
  // data:visible title
  props: ['data', 'editData'],
  data() {
    return {
      ruleForm: {
        title: '',
        overview: '',
      },
      rules: {
        title: [
          {required: true, message: '请输入通知标题', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    send() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.editData) { // 如果有edit数据说明是更新inform
            this.$store.dispatch('activity/updateInform', {
              ...this.ruleForm,
              id: this.editData.id
            }).then(
                () => {
                  this.$message.success('更新成功');
                  this.$bus.$emit('getInforms');
                  this.$bus.$emit('editDone',{...this.editData,...this.ruleForm})
                  this.data.visible = false;
                }
            )
          } else {
            this.$store.dispatch('activity/addInform', {
              ...this.ruleForm,
              activityId: this.data.activityId,
              society_id: this.$store.getters.society_id
            }).then(
                () => {
                  this.resetForm('ruleForm')
                  this.data.visible = false;
                  this.$message.success('成功')
                }
            )
          }
        } else {
          return false;
        }
      });
    },
  },
  watch: {
    editData: {
      deep: true,
      handler() {
        this.ruleForm.title = this.editData.title;
        this.ruleForm.needConfirm = this.editData.need_confirm * 1;
        this.ruleForm.overview = this.editData.overview;
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>